<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/15
 * Time: 9:27
 */

namespace app\common\lib;


use TencentCloud\Ivld\V20210903\Models\MediaPreknownInfo;

class TencentIvld
{
    public $secretid;

    public $secretkey;

    public $region;

    public $ivldClient;

    protected static $instance;

    public function __construct()
    {

        $config = config('param.tencent_cloud');
        $this->secretid = $config['secretid'];
        $this->secretkey = $config['secretkey'];
        $this->region = $config['ivld']['region'];

        $credential = new \TencentCloud\Common\Credential($this->secretid ,$this->secretkey);
        $this->ivldClient = new \TencentCloud\Ivld\V20210903\IvldClient($credential, $this->region);
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }


        return new self();
    }

    public function importMediaFile($videoLink)
    {
        $request = (new \TencentCloud\Ivld\V20210903\Models\ImportMediaRequest());
        $request->setURL($videoLink);
        $request->setName('ai_video_media'.date('ymd').'_'.uniqid());
        $result = $this->ivldClient->ImportMedia($request);

        return $result->serialize();
    }

    public function describeMedia($mediaId)
    {
        $request = (new \TencentCloud\Ivld\V20210903\Models\DescribeMediaRequest());
        $request->setMediaId($mediaId);
        $result = $this->ivldClient->DescribeMedia($request);

        return $result->serialize();
    }

    public function createTask($mediaId)
    {
        $request = (new \TencentCloud\Ivld\V20210903\Models\CreateTaskRequest());
        $request->setMediaId($mediaId);
        $mediaPreknownInfo = (new \TencentCloud\Ivld\V20210903\Models\MediaPreknownInfo());
        $mediaPreknownInfo->setMediaType(2);
        $mediaPreknownInfo->setMediaLabel(5);
        $mediaPreknownInfo->setMediaSecondLabel(2);
        $mediaPreknownInfo->setMediaLang(1);
        $request->setMediaPreknownInfo($mediaPreknownInfo);
        $request->setTaskName('ai_video_task'.date('ymd').'_'.uniqid());
        $result = $this->ivldClient->CreateTask($request);
        return $result->serialize();
    }

    public function describeTask($taskId)
    {
        $request = (new \TencentCloud\Ivld\V20210903\Models\DescribeTaskRequest());
        $request->setTaskId($taskId);
        $result = $this->ivldClient->DescribeTask($request);
        return $result->serialize();
    }

    public function describeTaskDetail($taskId)
    {
        $request = (new \TencentCloud\Ivld\V20210903\Models\DescribeTaskDetailRequest());
        $request->setTaskId($taskId);
        $result = $this->ivldClient->DescribeTaskDetail($request);
        return $result->serialize();
    }

}